package main

import (
	"fmt"
	"net"
	"socket"
	"time"
)

// 客户端
func main() {

	//调用Dial连接服务端
	dial, err := net.Dial("tcp", socket.GetCon())
	if err != nil {
		fmt.Println("client server error", err)
	}
	for { //不断发送
		time.Sleep(1 * time.Second)
		//调用Write写数据
		n, err := dial.Write([]byte("hell world"))

		if err != nil {
			fmt.Println("write data to server error", err)
		}

		fmt.Println("send data to server success ! the data length is ", n)

		//读取服务器响应

		buffer := make([]byte, 1024)

		//响应长度
		n, err = dial.Read(buffer)

		if err != nil {
			fmt.Println("receive response from server error", err)
		}

		fmt.Println("receive response from server success,the data length is ", n)

		//获取返回数据,有1024个字节，但是服务端可能就返回四个字节，所以截取
		response := string(buffer[:n])

		fmt.Println("server response is ", response)
		func() {
			closeErr := dial.Close()
			if closeErr != nil {
				fmt.Println("close error")
			}
		}()
	}

}
